#include <QFile>

#include "common/logger/logger.h"
#include "menuwidget.h"
#include "ui_menuwidget.h"

#include <QAction>


MenuWidget::MenuWidget(QWidget* parent)
	: QWidget(parent), ui(new Ui::MenuWidget)
{
	ui->setupUi(this);
	init();
}

MenuWidget::~MenuWidget()
{
	delete ui;
}
void MenuWidget::init()
{
	this->setObjectName("MenuWidget");

	QFile file(":/style/menuWidget.qss");
	if (file.open(QFile::ReadOnly))
	{
		this->setStyleSheet(file.readAll());
	}

	initTopLevelMenu();
}
void MenuWidget::initTopLevelMenu()
{
	for (int i = 0; i < 10; ++i)
	{
		auto* act = new QAction;
		act->setData(i);
		act->setText(QString("PACS%1").arg(i).toUpper());
		QIcon icon;
		icon.addPixmap(QPixmap{":/icon/menu/pacs.png"}, QIcon::Normal, QIcon::Off);
		icon.addPixmap(QPixmap{":/icon/menu/pacs_active.png"}, QIcon::Normal, QIcon::On);
		act->setIcon(icon);
		ui->topmenu->addItem(act);
	}
	connect(ui->topmenu,&CxiTopLevelMenu::itemClicked,this,[&](QAction* act) {
		auto sideMenu = ui->sidemenu;
		sideMenu->clearMenu();
		auto data = act->data().toString();

		QList<QTreeWidgetItem*> items;
		for (int i = 0; i < 10; ++i)
		{
			QTreeWidgetItem* item = new QTreeWidgetItem;
			for (int j = 0;j < 5;++ j)
			{
				QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
				subItem->setText(0,QString("子组%1").arg(i));
			}
			item->setText(0, data + "父组");
			items.append(item);
		}
		sideMenu->buildMenu(items);
 	});
}
